package g

/*
 * @lc app=leetcode.cn id=102 lang=golang
 *
 * [102] 二叉树的层序遍历
  树中节点数目在范围 [0, 100] 内
-100 <= Node.val <= 100

*/

// @lc code=start
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */

//          1
//         / \
//        2   3
//       / \
//      4   5
//     /     \
//    6       8
//     \       \
//      7       9
// return [][]int{[1],[2,3],[4,5],[6,8],[7,9]}
func levelOrder(root *TreeNode) [][]int {
	var ret [][]int
	if root == nil {
		return ret
	}
	var curLevelNodes []*TreeNode
	curLevelNodes = append(curLevelNodes, root)
	for len(curLevelNodes) > 0 {
		var newLevelNodes []*TreeNode
		var curRet []int
		for _, node := range curLevelNodes {
			curRet = append(curRet, node.Val)
			if node.Left != nil {
				newLevelNodes = append(newLevelNodes, node.Left)
			}
			if node.Right != nil {
				newLevelNodes = append(newLevelNodes, node.Right)
			}
		}
		ret = append(ret, curRet)
		curLevelNodes = newLevelNodes
	}

	return ret
}

// @lc code=end
